#include "oGamePreDefine.h"
#include "Actions/oSwitchAction.h"
#include "oGameEvent.h"
#include "oGameTypes.h"

string oSwitchIdle::AnimationIdle("idle");

oSwitchIdle::oSwitchIdle( oUnit* unit ):
oAction(ACTION_SWITCH_IDLE, 0, unit),
_current(0)
{ }

void oSwitchIdle::run()
{
	oAction::run();
	oModel* model = _owner->getModel();
	model->setLook(0);
	model->setLoop(true);
	model->play(AnimationIdle);
	_current = 0;
	if (actionStart != NULL)
	{
		actionStart(this);
	}
}

void oSwitchIdle::update( ccTime dt )
{
	_current += dt;
	if (_current > 0.05f)
	{
		_current = 0;
		BOOST_FOREACH(oBody* body, _owner->getAttackSensor()->getSensedBodies())
		{
			if (oSharedData.isPlayerUnit(body))
			{
				oUnit* unit = (oUnit*)body;
				if (unit->getTag() == TAG_BUNNY || unit->getTag() == TAG_HERO)
				{
					if (oSharedData.getRelation(unit, _owner) == e_friend)
					{
						int switchTag = _owner->getTag();
						bool switchAtRight = _owner->getPositionX() > unit->getPositionX();
						float flag = oGetSwitchFlag(switchTag, switchAtRight);
						unit->properties[oStr::IsAtSwitch] = flag;
					}
				}
			}
		}
	}
}

void oSwitchIdle::stop()
{
	oAction::stop();
	oModel* model = _owner->getModel();
	model->stop();
	BOOST_FOREACH(oBody* body, _owner->getAttackSensor()->getSensedBodies())
	{
		if (oSharedData.isPlayerUnit(body))
		{
			oUnit* unit = (oUnit*)body;
			if (unit->getTag() == TAG_BUNNY || unit->getTag() == TAG_HERO)
			{
				if (oSharedData.getRelation(unit, _owner) == e_friend)
				{
					unit->properties[oStr::IsAtSwitch] = 0;
				}
			}
		}
	}
	if (actionEnd != NULL)
	{
		actionEnd(this);
	}
}

string oSwitchPush::AnimationTurn("turn");

oSwitchPush::oSwitchPush( oUnit* unit ):
oAction(ACTION_SWITCH_PUSH, 1, unit)
{
	oModel* model = _owner->getModel();
	_turnAnimationIndex = model->getModelDef()->getActionIndexByName(AnimationTurn);
	model->animationEnd = oAnimationHandler(&oSwitchPush::onAnimationEnd, this);
	_listener = oSwitchEvent::addListener(
		oGameEventName::PushSwitch,
		oEventHandler(&oSwitchPush::onSwitchEvent, this));
	_listener->retain();
}

oSwitchPush::~oSwitchPush()
{
	CC_SAFE_RELEASE_NULL(_listener);
}

void oSwitchPush::run()
{
	oAction::run();
	oModel* model = _owner->getModel();
	model->setLook(0);
	model->setLoop(false);
	model->play(AnimationTurn);
	CCSprite* bottom = (CCSprite*)model->getChildren()->objectAtIndex(1);
	bottom->setScaleX(_owner->isFaceRight() ? 1.0f : -1.0f);
	SimpleAudioEngine::sharedEngine()->playEffect("sound/switch.wav");
	if (actionStart != NULL)
	{
		actionStart(this);
	}
}

void oSwitchPush::update( ccTime dt )
{ }

void oSwitchPush::stop()
{
	oAction::stop();
	oModel* model = _owner->getModel();
	model->stop();
	_owner->setFaceRight(true);
	CCSprite* bottom = (CCSprite*)model->getChildren()->objectAtIndex(1);
	bottom->setScaleX(1.0f);
	if (actionEnd != NULL)
	{
		actionEnd(this);
	}
}

void oSwitchPush::onAnimationEnd( oModel* model, uint32 index )
{
	if (index == _turnAnimationIndex)
	{
		oSwitchPush::stop();
		_owner->doIt(ACTION_SWITCH_IDLE);
	}
}

void oSwitchPush::onSwitchEvent( oEvent* e )
{
	oSwitchEvent* event = (oSwitchEvent*)e;
	bool mySide = _owner->getGroup() == GROUP_ME;
	bool switchSide = event->isMe;
	if (event->switchId == _owner->getTag() && mySide == switchSide)
	{
		_owner->setFaceRight(event->switchRight);
		_owner->doIt(ACTION_SWITCH_PUSH);
	}
}
